﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using QSTOOL.Codes;

namespace QSTOOL.Forms
{
    public partial class FormCommandMaintain : Form
    {
        private ArrayList allCommands { get; set; }
        public XMLTool xml { get; set; }
        public static bool Modified = false;

        public FormCommandMaintain()
        {
            InitializeComponent();
        }

        public FormCommandMaintain(ArrayList commands)
        {
            this.allCommands = commands;
        }

        private void FormCommandMaintain_Load(object sender, EventArgs e)
        {
            LoadCommands();
        }

        /// <summary>
        /// Load commands to the ListView
        /// </summary>
        private void LoadCommands()
        {
            this.allCommands = this.xml.allcommands;
            this.lsvCommandList.Items.Clear();
            
            for (int i = 0; i < this.allCommands.Count; i++)
            {
                List<Command> step_commands = (List<Command>)allCommands[i];
                foreach (Command cmd in step_commands)
                {
                    ListViewItem lvItem = new ListViewItem();
                    lvItem.Group = lsvCommandList.Groups[i];

                    ListViewItem.ListViewSubItem lvsiCommand = new ListViewItem.ListViewSubItem();
                    lvsiCommand.Name = QSTOOL.Codes.Attribute.COMMAND;
                    lvsiCommand.Text = cmd.command;

                    ListViewItem.ListViewSubItem lvsiDetail = new ListViewItem.ListViewSubItem();
                    lvsiDetail.Name = QSTOOL.Codes.Attribute.DETAIL;
                    lvsiDetail.Text = cmd.detail;

                    ListViewItem.ListViewSubItem lvsiSource = new ListViewItem.ListViewSubItem();
                    lvsiSource.Name = QSTOOL.Codes.Attribute.SOURCE;
                    lvsiSource.Text = cmd.source;

                    ListViewItem.ListViewSubItem lvsiTarget = new ListViewItem.ListViewSubItem();
                    lvsiTarget.Name = QSTOOL.Codes.Attribute.TARGET;
                    lvsiTarget.Text = cmd.target;

                    ListViewItem.ListViewSubItem lvsiDescription = new ListViewItem.ListViewSubItem();
                    lvsiDescription.Name = QSTOOL.Codes.Attribute.DESCRIPTION;
                    lvsiDescription.Text = cmd.description;

                    ListViewItem.ListViewSubItem lvsiGUID = new ListViewItem.ListViewSubItem();
                    lvsiGUID.Name = QSTOOL.Codes.Attribute.GUID;
                    lvsiGUID.Text = cmd.guid;

                    lvItem.SubItems.Add(lvsiCommand);
                    lvItem.SubItems.Add(lvsiDetail);
                    lvItem.SubItems.Add(lvsiSource);
                    lvItem.SubItems.Add(lvsiTarget);
                    lvItem.SubItems.Add(lvsiDescription);
                    lvItem.SubItems.Add(lvsiGUID);

                    this.lsvCommandList.Items.Add(lvItem);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Insert(Operation.APPEND);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            Modify();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要保存到配置文件吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.xml.Save();
                MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lsvCommandList_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (this.lsvCommandList.SelectedItems.Count > 0)
                {                    
                    this.contextMenuStrip.Show(this, e.Location);
                }
            }
        }

        private void contextmenuInsertBefore_Click(object sender, EventArgs e)
        {
            Insert(Operation.INSERT_BEFORE);
        }

        private void contextmenuInsertAfter_Click(object sender, EventArgs e)
        {
            Insert(Operation.INSERT_AFTER);
        }

        private void contextmenuModify_Click(object sender, EventArgs e)
        {
            Modify();
        }

        private void contextmenuDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private string GetSelectedGUID()
        {
            return lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.GUID].Text;
        }

        private Command GetSelectedCommand()
        {
            Command cmd = new Command();
            cmd.command = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.COMMAND].Text;
            cmd.detail = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.DETAIL].Text;
            cmd.source = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.SOURCE].Text;
            cmd.target = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.TARGET].Text;
            cmd.description = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.DESCRIPTION].Text;
            cmd.guid = lsvCommandList.SelectedItems[0].SubItems[QSTOOL.Codes.Attribute.GUID].Text;

            return cmd;
        }

        private void Insert(int type)
        {
            string ref_guid = "";
            FormCommandMaintainModify frmCMM = new FormCommandMaintainModify();
            
            switch (type)
            { 
                case Operation.APPEND:
                    frmCMM.type = Operation.APPEND;
                    break;
                case Operation.INSERT_AFTER:
                    frmCMM.type = Operation.INSERT_AFTER;
                    ref_guid = GetSelectedGUID();
                    break;
                case Operation.INSERT_BEFORE:
                    frmCMM.type = Operation.INSERT_BEFORE;
                    ref_guid = GetSelectedGUID();
                    break;
            }
            frmCMM.operation = Operation.INSERT;
            frmCMM.xml = this.xml;
            frmCMM.ref_GUID = ref_guid;
            frmCMM.ShowDialog();
            LoadCommands();
        }

        private void Modify()
        {
            if (this.lsvCommandList.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            FormCommandMaintainModify frmCMM = new FormCommandMaintainModify();
            Command cmd = GetSelectedCommand();
            frmCMM.operation = Operation.MODIFY;
            frmCMM.xml = this.xml;
            frmCMM.command = cmd;
            frmCMM.ShowDialog();
            LoadCommands();
        }

        private void Delete()
        {
            if (this.lsvCommandList.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确定删除选中的命令吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string guid = GetSelectedGUID();
                this.xml.Delete(guid);
                MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadCommands();
            }
        }

        private void FormCommandMaintain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Modified)
            {
                if (MessageBox.Show("有修改尚未保存，保存并退出吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.xml.Save();
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
